home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et-2_2.lha / et2.2 / src / Storage.C < prev    next >
C/C++ Source or Header  |  1990-11-29  |  2KB  |  120 lines

  1. #include "Storage.h"
  2. #include "System.h"
  3. #include "Error.h"
  4. #include "FixedStorage.h"
  5.  
  6. #include "MALLOC/storage.h"
  7.  
  8. const int cMaxSize= 300;
  9. static bool init= FALSE;
  10. static void *smallestAddress= 0;
  11. static int maxA= 0;
  12. static int ASizes[cMaxSize];
  13. static int FSizes[cMaxSize];
  14. static int totalA, totalF;
  15.  
  16. FreeHookFun storageFreeHook= 0;
  17. void *storageFreeHookData;
  18.      
  19. void StorageSetFreeHook(FreeHookFun fh, void *data)
  20. {
  21.     storageFreeHook= fh;
  22.     storageFreeHookData= data;
  23. }
  24.  
  25. FreeHookFun StorageGetFreeHook(void **data)
  26. {
  27.     *data= storageFreeHookData;
  28.     return storageFreeHook;
  29. }
  30.         
  31. static void StorageInit()
  32. {
  33.     init= TRUE;
  34.     storage_init();
  35.     // smallestAddress= Malloc(1);
  36. }
  37.  
  38. inline void InitStorage()
  39. {
  40.     if (!init)
  41.     StorageInit();
  42. }
  43.  
  44. static void EnterStat(int size)
  45. {
  46.     if (size >= cMaxSize)
  47.     ASizes[cMaxSize-1]++;
  48.     else
  49.     ASizes[size]++;
  50.     totalA+= size;
  51. }
  52.  
  53. static void RemoveStat(int size)
  54. {
  55.     if (size >= cMaxSize)
  56.     FSizes[cMaxSize-1]++;
  57.     else
  58.     FSizes[size]++;
  59.     totalF+= size;
  60. }
  61.  
  62. void* operator new(size_t size)
  63. {
  64.     InitStorage();
  65.     if (size < 0)
  66.     Fatal("operator new", "size < 0");
  67.     maxA= max(maxA, size);
  68.     EnterStat(size);
  69.     return storage_malloc(size);
  70. }
  71.  
  72. void operator delete(void* ptr)
  73. {
  74.     if (ptr) {
  75.     if (ptr < smallestAddress)
  76.         Fatal("Free", "unreasonable address (0x%x)", ptr);
  77.     int size= storage_size(ptr);
  78.     if (size < 0 || size > maxA)
  79.         Fatal("Free", "unreasonable size (%d)", size);
  80.     RemoveStat(size);
  81.     if (storageFreeHook) 
  82.         storageFreeHook(storageFreeHookData, ptr, size);
  83.     storage_free(ptr, size);
  84.     }
  85. }
  86.  
  87. void *Realloc(void *ptr, size_t size)
  88. {
  89.     InitStorage();
  90.     maxA= max(maxA, size);
  91.     if (ptr == 0) {
  92.     EnterStat(size);
  93.     ptr= storage_malloc(size);
  94.     } else if (ptr < smallestAddress) {
  95.     Fatal("Realloc", "unreasonable address (0x%x)", ptr);
  96.     } else {
  97.     int oldsize= storage_size(ptr);
  98.     if (size > oldsize) {
  99.         RemoveStat(oldsize);
  100.         EnterStat(size);
  101.         ptr= storage_realloc(ptr, oldsize, size);
  102.     }
  103.     }
  104.     return ptr;
  105. }
  106.  
  107. void PrintStorageStatistics()
  108. {
  109.     cerr NL;
  110.     cerr << "  Size Alloc  Free  Diff\n";
  111.     cerr << "------------------------\n";
  112.     for (int i= 0; i < cMaxSize; i++)
  113.     if (ASizes[i] != FSizes[i])
  114.         cerr.form("%6d%6d%6d%6d\n", i, ASizes[i], FSizes[i],
  115.                             ASizes[i]-FSizes[i]);
  116.     cerr.form("\n\ntotalA: %d  totalF: %d totalD: %d\n", totalA, totalF,
  117.                             totalA-totalF);
  118. }
  119.  
  120.